/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Service;

import DAO.IMemberDao;
import java.util.List;
import javabeans.GroupPlan;
import javabeans.Member;
import DAO.DAO;
import DAO.IGroupPlanImpl;
import DAO.IMemberDaoImpl;
import DAO.IPlanDaoImpl;
import javabeans.Plan;

/**
 *
 * @author liweijiao
 */
public class MemberServiceImpl implements MemberService {

    IMemberDaoImpl memberDao = new IMemberDaoImpl();
    PlanServiceImpl planService = new PlanServiceImpl();
    GroupPlanServiceImpl groupplanService = new GroupPlanServiceImpl();
    IGroupPlanImpl groupplanDao = new IGroupPlanImpl();

    public boolean register(String email, String password, String firstname, String lastname, String gender) {
        Member member = new Member();
        member.setEmail(email);
        member.setPassword(password);
        member.setGender(gender);
        member.setFirstname(firstname);
        member.setLastname(lastname);
        if (memberDao.getMember(email) == null) {
            memberDao.createMember(member);
            DAO.close();
            return true;
        } else {
            DAO.close();
            return false;
        }



    }

    public int login(String email, String password) {
        Member memberl = new Member();
        memberl = memberDao.getMember(email);
        DAO.close();
        if (memberl != null) {
            if (memberl.getPassword().equals(password)) {
                return 0;//login successfully
            } else {
                return 1;//incorrect password
            }

        } else {
            return 2;// non-existed member
        }
    }

    public boolean checkEmail(String email) {
        Member memberl = new Member();
        memberl = memberDao.getMember(email);
        DAO.close();
        if (memberl != null) {
            return true;// Email exists in DB
        } else {
            return false; //Email not exists in DB
        }
    }

    public Member getMember(String email) {
        Member member1 = new Member();
        member1 = memberDao.getMember(email);
        DAO.close();
        return member1;
    }

    public Plan addPlan(String planname, String descrip, String userid) {
        Plan plan = planService.newPlan(planname, descrip, userid);
        DAO.close();
        return plan;
    }

    public GroupPlan createGroupPlan(String planname, String descrip, String email) {
        GroupPlan groupplan = groupplanService.newGroupPlan(planname, descrip);
        groupplan.getGroupmembers().add(memberDao.getMember(email));
        Member m = memberDao.getMember(email);
        m.getGroups().add(groupplan);
        memberDao.updateMember(m);
        DAO.close();
        return groupplan;
    }

    public List<GroupPlan> getGroupPlanList(int userID) {
        IGroupPlanImpl groupplanDao = new IGroupPlanImpl();
        List<GroupPlan> groupplanlist = groupplanDao.getGroupPlanbyUserId(userID);
        DAO.close();
        return groupplanlist;
    }

    public boolean joinGroup(Member member, GroupPlan groupplan) {
        member.getGroups().add(groupplan);
        groupplan.getGroupmembers().add(member);
        //memberDao.updateMember(member);
        groupplanDao.updateGroupPlan(groupplan);
        DAO.close();
        return true;
      /*  if (memberDao.updateMember(member)) {

            if (groupplan.getGroupmembers().add(member)) {
                groupplanDao.updateGroupPlan(groupplan);
                DAO.close();
                return true;
            } else {
                DAO.close();
                return false;
            }
        } else {
            {
                DAO.close();
                return false;
            }
        }*/
    }

    public boolean leaveGroup(Member member, GroupPlan groupplan) {
        //member.getGroups().remove(groupplan);
        int id = groupplan.getGroupplanid();
        List<GroupPlan> groupList = member.getGroups();
        System.out.println("size: " + groupList.size());
        GroupPlan gp;
        for(int i=0;i<groupList.size();i++)
        {
            gp = groupList.get(i);
            if(gp==null) System.out.println("gp is null");
            if(gp.getGroupplanid()==id)
            {
               member.getGroups().remove(i);
            }
        }
       // groupplan.getGroupmembers().remove(member);
        for(int i=0;i<groupplan.getGroupmembers().size();i++)
        {
          if(groupplan.getGroupmembers().get(i).getId()==member.getId())
          {
               groupplan.getGroupmembers().remove(i);
          }
        }
        memberDao.updateMember(member);
        groupplanDao.updateGroupPlan(groupplan);
        DAO.close();
        return true;
    }
}
